package jp.kentaro.plugin.memoryimageview.views;

import java.util.ArrayList;
import java.util.List;

import memoryimage.models.ByteDataArray;
import memoryimage.models.ByteDataArrayListener;
import memoryimage.models.MemoryImage;
import memoryimage.provider.MemoryContentProvider;
import memoryimage.provider.MemoryLabelProvider;
import memoryimage.provider.MemoryModifier;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import socketinterface.ServerController;

public class ViewPart extends org.eclipse.ui.part.ViewPart {
	public Display       display;
	Image         image;
	MemoryImage   memoryImage;
	public   Text          consoleBox;
	List<String>  logMessages;
	ServerController serverController;
	
	public ViewPart() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method
		display    = parent.getDisplay();
		consoleBox = new Text(parent,SWT.MULTI|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
		logMessages = new ArrayList<String>();
	    GridData gridData1 = new GridData();
	    gridData1.horizontalAlignment = GridData.FILL;
	    gridData1.grabExcessHorizontalSpace = true;
	    consoleBox.setLayoutData(gridData1);

	    //consoleText.add("aiueo");
		//consoleBox.setText(consoleText.get(0));
	    
		Table table = new Table(parent, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		// Create Header of Table
		String[] labels = new String[] { "Address(h)", "+0", "+1", "+2", "+3", "+4", "+5", "+6", "+7", "+8", "+9", "+A", "+B", "+C", "+D", "+E", "+F"};
		for (String label : labels) {
			TableColumn column = new TableColumn(table, SWT.LEFT);
			column.setText(label);
			column.pack();
		}
		
		// Create initial data of MemoryImage
		ByteDataArray byteDataArray = null;
		memoryImage = new MemoryImage();
		byteDataArray = new ByteDataArray(256);
		for(int i = 0; i < 0xFF; i++) {
			if(i != 0 && (i%16 == 0)) {
				memoryImage.add(byteDataArray);
				byteDataArray = new ByteDataArray(16);
			}
			byteDataArray.cells[i % 16] = (byte)i;
		}
		byteDataArray.cells[15] = (byte)0xFF;
		memoryImage.add(byteDataArray);
		
		// Set Editor
		CellEditor[] editors = new CellEditor[labels.length];
		for (int i = 0; i < labels.length; i++) {
			editors[i] = new TextCellEditor(table);
		}
	
		// Set ContentProvider
		TableViewer viewer = new TableViewer(table);
		viewer.setContentProvider(new MemoryContentProvider());
		
		// Set LabelProvider
		viewer.setLabelProvider(new MemoryLabelProvider(parent.getDisplay()));
		
		// Set CellEditor and CellModifier
		viewer.setColumnProperties(labels);
		viewer.setCellEditors(editors);
		viewer.setCellModifier(new MemoryModifier(viewer));
		
		// Set MemoryImage into the table viewer
		viewer.setInput(memoryImage);

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
